/**
 * This is a listener for planet
 */
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;

/**
 * This is a listener for planet
 * @author Master
 * @version $Revision: 1.0 $
 */
public class PlanetListener implements MouseListener{
	
	/**
	 * Field LINGDIANBA.
	 * (value is 0.8)
	 */
	private static final double LINGDIANBA = 0.8;
	
	/**
	 * Field LIANGQIAN.
	 * (value is 2000)
	 */
	private static final int LIANGQIAN = 2000;
	
	/**
	 * Field SHI.
	 * (value is 10)
	 */
	private static final int SHI = 10;
	
	/**
	 * Field JIU.
	 * (value is 9)
	 */
	private static final int JIU = 9;

	/**
	 * Method toString.
	
	 * @return String */
	@Override
	public String toString() {
		return "PlanetListener []";
	}

	/**
	 * Method Distance.
	 * @param a Planet
	 * @param b Planet
	
	 * @return int */
	public int distance(Planet a, Planet b){
		if (a == null || b == null) return 0;
		return (int) Math.sqrt((a.getX() - b.getX()) *
			   (a.getX() - b.getX()) + (a.getY() -
			   b.getY()) * (a.getY() - b.getY()));
	}

	/**
	 * Method Move.
	 * @param planet Planet
	 */
	public void move(Planet planet) {
		if (PlayerInfo.getDestination() != null) return;
		if (PlayerInfo.getFuel() >= distance(planet, PlayerInfo.getCurrentPlanet())){
			PlayerInfo.setDestination(planet);
			PlayerInfo.Cnt=0;
			GameInfo.CurrentPlanetListener=this;
		}else {
			JOptionPane.showMessageDialog(
			    ConfigurationScreen.MyFrame,
			    "You don't have enough fuel to travel that far");
		}
	}
	
	/**
	 * Method go.
	 * @param planet Planet
	 */
	public void go(Planet planet){
		PlayerInfo.setDestination(null);
		PlayerInfo.setFuel(PlayerInfo.getFuel() -
			distance(planet, PlayerInfo.getCurrentPlanet()));
		PlayerInfo.setCurrentPlanet(planet);
		PlayerInfo.setCurrentSolarSystem(planet.getSolarSystem());
		if (GameInfo.Rand.nextDouble() < planet.getChanceOfGetMoney()){
			strategy4(planet);
		}
		else if (GameInfo.Rand.nextDouble() < planet.getChanceToEncounterPirates()){
			if (GameInfo.Rand.nextDouble() < planet.getChanceOfSavedByPolice()){
				strategy1();
			}
			else{
				if (GameInfo.Rand.nextDouble() < planet.getChanceOfBeatPirates()) {
					strategy2(planet);
				}
				else {
					strategy3();
				}
			}
		}else if (GameInfo.Rand.nextInt(JIU) == 0){
			JOptionPane.showMessageDialog(
				ConfigurationScreen.MyFrame,
				"You founded a mysterious temple in this planet," +
				" you must solve the puzzle to get the treasure inside");
			LightsMain.run();
		}else if (GameInfo.Rand.nextInt(JIU) == 0){
			JOptionPane.showMessageDialog(
				ConfigurationScreen.MyFrame,
				"You encountered a lot of ducks while landing!");
			PlayerInfo.DuckKilled=-1;
			DuckHuntMain.run();
		}
		MarketPlaceScreen.start(planet);
		if (LightsMain.Frame != null) LightsMain.Frame.toFront();
		if (DuckHuntMain.Frame != null) DuckHuntMain.Frame.toFront(); 
	}
	
	/**
	 * Method mouseClicked.
	 * @param e MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseClicked(MouseEvent) */
	@Override
	public void mouseClicked(MouseEvent e) {
		final Point point = e.getPoint();
		for (SolarSystem solarSystem:Universe.SolarSystems)
			for (Planet planet: solarSystem.getPlanets()){
				if (Math.abs(point.x - planet.getX()) 
					<= SHI && Math.abs(point.y - planet.getY()) <= SHI){
					move(planet);
					return;
				}
			}
		
	}

	/**
	 * Method mousePressed.
	 * @param e MouseEvent
	
	 * @see java.awt.event.MouseListener#mousePressed(MouseEvent) */
	@Override
	public void mousePressed(MouseEvent e) {
		new ExitInfoListener();
	}

	/**
	 * Method mouseReleased.
	 * @param e MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseReleased(MouseEvent) */
	@Override
	public void mouseReleased(MouseEvent e) {
		new ExitInfoListener();
	}

	/**
	 * Method mouseEntered.
	 * @param e MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseEntered(MouseEvent) */
	@Override
	public void mouseEntered(MouseEvent e) {
		new ExitInfoListener();
	}

	/**
	 * Method mouseExited.
	 * @param e MouseEvent
	
	 * @see java.awt.event.MouseListener#mouseExited(MouseEvent) */
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		new ExitInfoListener();
	}
	
	/**
	 * Method strategy1.
	 */
	public void strategy1(){
		JOptionPane.showMessageDialog(
			ConfigurationScreen.MyFrame,
			"Unfortunately, you encountered pirates.\n" +
			"However, the police saved you.");
	}
	
	/**
	 * Method strategy2.
	 * @param planet Planet
	 */
	public void strategy2(Planet planet){
		final int val=GameInfo.Rand.nextInt(LIANGQIAN);
		final int moneyRobbed=(int) (Math.min(planet.getMoneyRobbedByPirates() *
				PlayerInfo.Money, val));
		if (PlayerInfo.canGetInsurance()){
			final int moneyReturn=(int) (moneyRobbed * LINGDIANBA);
			JOptionPane.showMessageDialog(
				ConfigurationScreen.MyFrame,
				"Unfortunately, you encountered pirates.\n" +
				"You got robbed $" + moneyRobbed + 
				". \nBut you get &" + moneyReturn + " back by insurance");
		}
		else{
			PlayerInfo.Money-=moneyRobbed;
			JOptionPane.showMessageDialog(
				ConfigurationScreen.MyFrame,
				"Unfortunately, you encountered pirates.\n" +
				"You got robbed $" + moneyRobbed);
		}
	}

	/**
	 * Method strategy3.
	 */
	public void strategy3(){
		JOptionPane.showMessageDialog(
			ConfigurationScreen.MyFrame,
			"Unfortunately, you encountered priates.\n However, you beated them!");
	}
	
	/**
	 * Method strategy4.
	 * @param planet Planet
	 */
    public void strategy4(Planet planet){
		final int val=GameInfo.Rand.nextInt(LIANGQIAN);
		final int moneyGained=(int) (
			Math.min(planet.getMoneyRobbedByPirates() * PlayerInfo.Money, val));
		PlayerInfo.Money+=moneyGained;
		JOptionPane.showMessageDialog(
			ConfigurationScreen.MyFrame,
			"Luckily, you found an abandoned ship.\n" +
			"You gained $" + moneyGained);
	}
}
